package com.lc.utils.download;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DownloadUtils {
	public static void main(String[] args) {
		String url = "http://www.4493.com/tiyumeinv/24526/1.htm";
		String encoding = "utf-8";
		String html = getResourceUrl(url, encoding);
		Document document = Jsoup.parse(html);
		Elements elements = document.getElementsByTag("img");
		for (Element ele : elements) {
			String src = ele.attr("src");
			if (src != null && !"".equals(src) && src.startsWith("http://")) {
				String fileName = src.substring(src.lastIndexOf("/"), src.length());
				try {
					getImages(src, "c:\\test\\" + fileName);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println(src);
			}
		}

	}

	/**
	 * @param urlPath
	 *            图片路径
	 * @throws Exception
	 */
	public static void getImages(String urlPath, String fileName) throws Exception {
		URL url = new URL(urlPath);// ：获取的路径
		// :http协议连接对象
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setReadTimeout(6 * 10000);
		if (conn.getResponseCode() < 10000) {
			InputStream inputStream = conn.getInputStream();
			byte[] data = readStream(inputStream);
			if (data.length > (1024 * 10)) {
				FileOutputStream outputStream = new FileOutputStream(fileName);
				outputStream.write(data);
				outputStream.close();
			}
		}

	}

	/**
	 * 读取url中数据，并以字节的形式返回
	 * 
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] readStream(InputStream inputStream) throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while ((len = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, len);
		}
		outputStream.close();
		inputStream.close();
		return outputStream.toByteArray();
	}

	public static String getResourceUrl(String url, String encoding) {
		StringBuilder sb = new StringBuilder();
		URL urlObj = null;
		URLConnection conn = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			urlObj = new URL(url);
			conn = urlObj.openConnection();
			isr = new InputStreamReader(conn.getInputStream());
			br = new BufferedReader(isr);
			String temp = null;
			while ((temp = br.readLine()) != null) {
				sb.append(temp + "\r\n");
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return sb.toString();
	}
}
